import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import {
  SequelizeModule,
  type SequelizeModuleOptions,
} from '@nestjs/sequelize';
import { UserModule } from './user/user.module';
import { RoleModule } from './role/role.module';
import { PermissionModule } from './permission/permission.module';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { ResponseInterceptor } from './interceptors/response.interceptor';
import { UploadModule } from './upload/upload.module';
import { OssModule } from './oss/oss.module';
import configuration from './config/config';
import { ConfigModule, ConfigService } from '@nestjs/config';
import type { Dialect } from 'sequelize';

@Module({
  imports: [
    ConfigModule.forRoot({
      load: [configuration],
      isGlobal: true, // 设置为全局模块
    }),
    SequelizeModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (
        configService: ConfigService,
      ): Promise<SequelizeModuleOptions> => ({
        dialect: configService.get<string>('db.dialect') as Dialect,
        host: configService.get<string>('db.host'),
        port: parseInt(configService.get<string>('db.port'), 10),
        username: configService.get<string>('db.username'),
        password: configService.get<string>('db.password'),
        database: configService.get<string>('db.database'),
        autoLoadModels: configService.get<boolean>('db.autoLoadModels'),
        synchronize: configService.get<boolean>('db.synchronize'),
        // timezone: configService.get<string>('db.timezone'),
        // logging: configService.get<boolean>('db.logging'),
      }),
      inject: [ConfigService],
    }),
    AuthModule,
    UserModule,
    RoleModule,
    PermissionModule,
    OssModule,
    UploadModule,
  ],
  controllers: [AppController],
  providers: [
    {
      provide: APP_INTERCEPTOR,
      useClass: ResponseInterceptor,
    },
    AppService,
  ],
})
export class AppModule {}
